package com.unience.ec2.eclipse.treeviewer.ui;



import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;

import com.unience.aws.monitor.model.LoadInfo;

import com.unience.aws.monitor.model.MonitorInstanceInfo;
import com.unience.ec2.eclipse.config.Config;
import com.unience.ec2.eclipse.model.Ec2Image;
import com.unience.ec2.eclipse.model.SecurityGroup;
import com.unience.ec2.eclipse.model.Ec2Instance;
import com.unience.ec2.eclipse.model.Ec2Info;

public class MovingBoxLabelProvider extends LabelProvider {	
	
	
	private static Log log = LogFactory.getLog(MovingBoxLabelProvider.class);
	
	private Map imageCache = new HashMap(11);
	
	/*
	 * @see ILabelProvider#getImage(Object)
	 */
	public Image getImage(Object element) {
		ImageDescriptor descriptor = null;
		
		
			
	
		
		if (element instanceof Ec2Info) {
			descriptor =ImageDescriptor.createFromFile(Config.class,"sample2.gif" );
		} else if (element instanceof Ec2Instance) {
			
			Ec2Instance newName = (Ec2Instance)element;
			if ( newName.getEc2inst().getInstance().getStateCode() == 16){
				descriptor =ImageDescriptor.createFromFile(Config.class,"started.png" );
			}else if ( newName.getEc2inst().getInstance().getStateCode() == 48){
				descriptor =ImageDescriptor.createFromFile(Config.class,"terminated.png" );
			}else {
				descriptor =ImageDescriptor.createFromFile(Config.class,"pending.png" );
			}
			
			
		} else if (element instanceof SecurityGroup) {
			descriptor =ImageDescriptor.createFromFile(Config.class,"secgroup.png" );
		}else if (element instanceof Ec2Image){			
			descriptor =ImageDescriptor.createFromFile(Config.class,"image.png" );
		} 
		else {
			throw unknownElement(element);
		}

		if (descriptor == null){
			noImageDefined(element);
			return null;
		}
		//obtain the cached image corresponding to the descriptor
		Image image = (Image)imageCache.get(descriptor);
		if (image == null) {
			image = descriptor.createImage();
			imageCache.put(descriptor, image);
		}
		return image;
	}

	/*
	 * @see ILabelProvider#getText(Object)
	 */
	public String getText(Object element) {
		if (element instanceof Ec2Info) {
			if(((Ec2Info)element).getName() == null) {
				return "Box";
			} else {
				return ((Ec2Info)element).getName();
			}
		} else if (element instanceof Ec2Instance) {
			StringBuffer title =  new StringBuffer();
			Ec2Instance newName = (Ec2Instance)element;
			
			title.append((((Ec2Instance)element).getTitle())).append(" (").append(newName.getEc2inst().getInstance().getState()).append(")");
			
		 MonitorInstanceInfo monitorInfo = 	newName.getEc2inst().getMonitorInfo();
		 LoadInfo loadInfo = monitorInfo.getLoadInfo();
		 
		 title.append("-").append( loadInfo.getLoad1m()).append("-").append(loadInfo.getLoad5m()).append("-").append(loadInfo.getLoad15m());
		 
		
		 
		 
			return title.toString(); 
		} else if (element instanceof SecurityGroup) {
			return ((SecurityGroup)element).getTitle();
		} else if (element instanceof Ec2Image){			
			
			StringBuffer title =  new StringBuffer();
			
			title.append(((Ec2Image)element).getImageDescription().getImageId());
			title.append(" (").append(((Ec2Image)element).getImageDescription().getImageLocation());
			title.append(")");
			
			
			
			return title.toString();
		}
		else{
		
			throw unknownElement(element);
		}
	}

	public void dispose() {
		for (Iterator i = imageCache.values().iterator(); i.hasNext();) {
			((Image) i.next()).dispose();
		}
		imageCache.clear();
	}

	protected RuntimeException unknownElement(Object element) {
		return new RuntimeException("Unknown type of element in tree of type " + element.getClass().getName());
	}
	
	protected void noImageDefined(Object element) {
		//return new RuntimeException("Unknown type of element in tree of type " + element.getClass().getName());
	}

}
